<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 400px;
            margin: 50px auto;
            padding: 30px;
            background-color: white;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        label {
            font-size: 14px;
            color: #555;
            display: block;
            margin-bottom: 8px;
        }

        input[type="text"], input[type="password"], input[type="email"], input[type="phone"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }

        select {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }

        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #0056b3;
        }

        .error {
            color: red;
            font-size: 14px;
            margin-top: -8px;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>用户注册</h1>
    <form id="registrationForm" action="reg" method="post">
        <label for="username">用户名</label>
        <input type="text" id="username" name="username" placeholder="请输入用户名" required><br>
        <span class="error" id="usernameError"></span>

        <label for="password">密码</label>
        <input type="password" id="password" name="password" placeholder="请输入密码" required><br>
        <span class="error" id="passwordError"></span>

        <label for="role">角色</label>
        <select id="role" name="role" required>
            <option value="1">会员</option>
            <option value="2">管理员</option>
            <option value="3">超级管理员</option>
        </select><br>

        <label for="email">邮箱</label>
        <input type="email" id="email" name="email" placeholder="请输入邮箱" required><br>
        <span class="error" id="emailError"></span>

        <label for="phone">联系方式</label>
        <input type="text" id="phone" name="phone" placeholder="请输入联系方式" required><br>
        <span class="error" id="phoneError"></span>

        <input type="submit" value="注册">
    </form>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    // 表单验证
    function validateForm(event) {
        event.preventDefault();

        const username = $('#username').val().trim();
        const password = $('#password').val().trim();
        const email = $('#email').val();
        const phone = $('#phone').val();
        let isValid = true;
        // 清除上次错误提示
        $('.error').text('');

        // 验证用户名和密码
        if (!username || !password) {
            $('#usernameError').text('用户名和密码不能为空！');
            isValid = false;
        }

        // 邮箱格式验证
        const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
        if (!emailPattern.test(email)) {
            $('#emailError').text('请输入有效的邮箱地址！');
            isValid = false;
        }

        // 如果验证通过，提交表单数据
        if (isValid) {
            $.ajax({
                url: 'reg',
                type: 'POST',
                data: {
                    username: username,
                    password: password,
                    email: email,
                    phone: phone
                },
                dataType: 'json',
                success: function(response) {
                    if (response.success) {
                        alert(response.message);
                        window.location.href = 'login.html';
                    } else {
                        alert(response.message);
                    }
                },
                error: function(xhr, status, error) {
                    alert('请求失败：' + error);
                }
            });
        }
    }

    // 表单提交事件
    $('#registrationForm').on('submit', validateForm);
</script>

</body>
</html>
